﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Maticsoft.Common;

namespace Lawyer.Web
{
    public partial class archiveadd : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BindData();
            }
        }

        private void BindData()
        {
            string UserID = Session["UserID"].ToString();
            BindCustomInfo(UserID);
            BindAgencyInfo();
        }
        private void BindCustomInfo(string UserID)
        {
            Lawyer.BLL.CustomInfo custombll = new Lawyer.BLL.CustomInfo();
            List<Lawyer.Model.CustomInfo> customlist = custombll.GetModelList("");
            ddlCustom.DataSource = customlist;
            ddlCustom.DataTextField = "FullName";
            ddlCustom.DataValueField = "CustomID";
            ddlCustom.DataBind();
        }
        private void BindAgencyInfo()
        {
            Lawyer.BLL.AgencyInfo agencybll = new Lawyer.BLL.AgencyInfo();
            List<Lawyer.Model.AgencyInfo> list = agencybll.GetModelList("");
            ddlAgency.DataSource = list;
            ddlAgency.DataTextField = "Name";
            ddlAgency.DataValueField = "AgencyID";
            ddlAgency.DataBind();
        }
        protected void btnSave_Click(object sender, EventArgs e)
        {

            
        }

        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            #region validation
            string strErr = "";

            if (this.txtFullName.Text.Trim().Length == 0)
            {
                strErr += "FullName不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtBirthday.Text))
            {
                strErr += "Birthday格式错误！\\n";
            }
            if (this.txtTelphone.Text.Trim().Length == 0)
            {
                strErr += "Telphone不能为空！\\n";
            }
            if (this.txtFax.Text.Trim().Length == 0)
            {
                strErr += "Fax不能为空！\\n";
            }
            if (this.txtAddress.Text.Trim().Length == 0)
            {
                strErr += "Address不能为空！\\n";
            }
            if (this.txtCFullName.Text.Trim().Length == 0)
            {
                strErr += "CFullName不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtCBirthday.Text))
            {
                strErr += "CBirthday格式错误！\\n";
            }
            if (this.txtCTelphone.Text.Trim().Length == 0)
            {
                strErr += "CTelphone不能为空！\\n";
            }
            if (this.txtCFax.Text.Trim().Length == 0)
            {
                strErr += "CFax不能为空！\\n";
            }
            if (this.txtCEmail.Text.Trim().Length == 0)
            {
                strErr += "CEmail不能为空！\\n";
            }
            if (this.txtCRelation.Text.Trim().Length == 0)
            {
                strErr += "CRelation不能为空！\\n";
            }
            if (!PageValidate.IsDecimal(txtBankFunds.Text))
            {
                strErr += "BankFunds格式错误！\\n";
            }
            if (this.txtBankAccount.Text.Trim().Length == 0)
            {
                strErr += "BankAccount不能为空！\\n";
            }
            if (this.txtCaseSource.Text.Trim().Length == 0)
            {
                strErr += "CaseSource不能为空！\\n";
            }
            if (this.txtPersonalDetail.Text.Trim().Length == 0)
            {
                strErr += "PersonalDetail不能为空！\\n";
            }
            if (this.txtPersonalNote.Text.Trim().Length == 0)
            {
                strErr += "PersonalNote不能为空！\\n";
            }
            if (this.txtFatherName.Text.Trim().Length == 0)
            {
                strErr += "FatherName不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtFatherDOB.Text))
            {
                strErr += "FatherDOB格式错误！\\n";
            }
            if (this.txtFatherNationality.Text.Trim().Length == 0)
            {
                strErr += "FatherNationality不能为空！\\n";
            }
            if (this.txtFatherContact.Text.Trim().Length == 0)
            {
                strErr += "FatherContact不能为空！\\n";
            }
            if (this.txtMotherName.Text.Trim().Length == 0)
            {
                strErr += "MotherName不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtMotherDOB.Text))
            {
                strErr += "MotherDOB格式错误！\\n";
            }
            if (this.txtMotherNationality.Text.Trim().Length == 0)
            {
                strErr += "MotherNationality不能为空！\\n";
            }
            if (this.txtMotherContact.Text.Trim().Length == 0)
            {
                strErr += "MotherContact不能为空！\\n";
            }
            if (this.txtSisterName.Text.Trim().Length == 0)
            {
                strErr += "SisterName不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtSisterDOB.Text))
            {
                strErr += "SisterDOB格式错误！\\n";
            }
            if (this.txtSisterContact.Text.Trim().Length == 0)
            {
                strErr += "SisterContact不能为空！\\n";
            }
            if (this.txtSisterNationality.Text.Trim().Length == 0)
            {
                strErr += "SisterNationality不能为空！\\n";
            }
            if (this.txtBrotherName.Text.Trim().Length == 0)
            {
                strErr += "BrotherName不能为空！\\n";
            }
            if (!PageValidate.IsDateTime(txtBrotherDOB.Text))
            {
                strErr += "BrotherDOB格式错误！\\n";
            }
            if (this.txtBrotherNationality.Text.Trim().Length == 0)
            {
                strErr += "BrotherNationality不能为空！\\n";
            }
            if (this.txtBrotherContact.Text.Trim().Length == 0)
            {
                strErr += "BrotherContact不能为空！\\n";
            }
            if (!PageValidate.IsDecimal(txtAgreedFees.Text))
            {
                strErr += "AgreedFees格式错误！\\n";
            }
            if (this.txtPersonalStatement.Text.Trim().Length == 0)
            {
                strErr += "PersonalStatement不能为空！\\n";
            }
            if (this.txtLawyerNote.Text.Trim().Length == 0)
            {
                strErr += "LawyerNote不能为空！\\n";
            }
            if (this.txtAssetAName.Text.Trim().Length == 0)
            {
                strErr += "AssetAName不能为空！\\n";
            }
            if (!PageValidate.IsDecimal(txtAssetAWorth.Text))
            {
                strErr += "AssetAWorth格式错误！\\n";
            }
            if (this.txtAssetANote.Text.Trim().Length == 0)
            {
                strErr += "AssetANote不能为空！\\n";
            }
            if (this.txtAssetADetail.Text.Trim().Length == 0)
            {
                strErr += "AssetADetail不能为空！\\n";
            }
            if (this.txtAssetBName.Text.Trim().Length == 0)
            {
                strErr += "AssetBName不能为空！\\n";
            }
            if (!PageValidate.IsDecimal(txtAssetBWorth.Text))
            {
                strErr += "AssetBWorth格式错误！\\n";
            }
            if (this.txtAssetBNote.Text.Trim().Length == 0)
            {
                strErr += "AssetBNote不能为空！\\n";
            }
            if (this.txtAssetBDetail.Text.Trim().Length == 0)
            {
                strErr += "AssetBDetail不能为空！\\n";
            }
            if (this.txtAssetCName.Text.Trim().Length == 0)
            {
                strErr += "AssetCName不能为空！\\n";
            }
            if (!PageValidate.IsDecimal(txtAssetCWorth.Text))
            {
                strErr += "AssetCWorth格式错误！\\n";
            }
            if (this.txtAssetCNote.Text.Trim().Length == 0)
            {
                strErr += "AssetCNote不能为空！\\n";
            }
            if (this.txtAssetCDetail.Text.Trim().Length == 0)
            {
                strErr += "AssetCDetail不能为空！\\n";
            }


            if (strErr != "")
            {
                //MessageBox.Show(this, strErr);
               // return;
            }
            #endregion

            #region Submit ArchiveInfo 
            string Title = this.txbCaseTitle.Text;
            string CustomID = this.ddlCustom.SelectedValue;
            int IsAgency = this.ckbIsAgency.Checked ? 1 : 0;
            string AgencyID = null;
            if (IsAgency == 1) {
                AgencyID = this.ddlAgency.SelectedValue;
            }
            decimal AgencyRate = 0;
            try
            {
                AgencyRate = Convert.ToDecimal(this.txbAgencyRate.Text);
            }
            catch { }
            Lawyer.BLL.ArchiveInfo archivebll = new Lawyer.BLL.ArchiveInfo();
            Lawyer.Model.ArchiveInfo archive = new Lawyer.Model.ArchiveInfo();
            archive.ArchiveID = Guid.NewGuid().ToString();
            archive.Title = Title;
            archive.CustomID = CustomID;
            archive.IsAgency = IsAgency;
            archive.AgencyID = AgencyID;
            archive.AgencyRate = AgencyRate;
            archive.CreatedBy = Session["UserID"].ToString();
            archive.CreatedDate = DateTime.Now;
            archive.Status = 0;
            archive.UpdatedBy = Session["UserID"].ToString();
            archive.UpdatedDate = DateTime.Now;
            if (!archivebll.Add(archive)) {
                Maticsoft.Common.MessageBox.Show(this,"failed!");
                return;
            }
            #endregion


            #region Submit Archive Base Info
            string FullName = this.txtFullName.Text;
            string Telphone = this.txtTelphone.Text;
            string Fax = this.txtFax.Text;
            string Address = this.txtAddress.Text;
            string CFullName = this.txtCFullName.Text;
            
            string CTelphone = this.txtCTelphone.Text;
            string CFax = this.txtCFax.Text;
            string CEmail = this.txtCEmail.Text;
            string CRelation = this.txtCRelation.Text;
            decimal BankFunds = 0;
            try
            {
                BankFunds = decimal.Parse(this.txtBankFunds.Text);
            }
            catch { }
            string BankAccount = this.txtBankAccount.Text;
            string CaseSource = this.txtCaseSource.Text;
            string PersonalDetail = this.txtPersonalDetail.Text;
            string PersonalNote = this.txtPersonalNote.Text;
            string FatherName = this.txtFatherName.Text;
            string FatherNationality = this.txtFatherNationality.Text;
            string FatherContact = this.txtFatherContact.Text;
            string MotherName = this.txtMotherName.Text;
            string MotherNationality = this.txtMotherNationality.Text;
            string MotherContact = this.txtMotherContact.Text;
            string SisterName = this.txtSisterName.Text;

            string SisterContact = this.txtSisterContact.Text;
            string SisterNationality = this.txtSisterNationality.Text;
            string BrotherName = this.txtBrotherName.Text;

            string BrotherNationality = this.txtBrotherNationality.Text;
            string BrotherContact = this.txtBrotherContact.Text;
            decimal AgreedFees = 0;
            try
            {
                AgreedFees = decimal.Parse(this.txtAgreedFees.Text);
            }
            catch { }

            int FeesClusive = 0;
            try
            {
                FeesClusive = int.Parse(this.rblClusive.SelectedValue);
            }
            catch { }
            int FeesFundable = 0;
            try
            {
                FeesFundable = int.Parse(this.rblFundable.SelectedValue);
            }
            catch { }
            string PersonalStatement = this.txtPersonalStatement.Text;
            string LawyerNote = this.txtLawyerNote.Text;
            string AssetAName = this.txtAssetAName.Text;
            decimal AssetAWorth = 0;
            try
            {
                AssetAWorth = decimal.Parse(this.txtAssetAWorth.Text);
            }
            catch { }
            string AssetANote = this.txtAssetANote.Text;
            string AssetADetail = this.txtAssetADetail.Text;
            string AssetBName = this.txtAssetBName.Text;
            decimal AssetBWorth = 0;
            try {
                AssetBWorth = decimal.Parse(this.txtAssetBWorth.Text);
            }
            catch { }
            string AssetBNote = this.txtAssetBNote.Text;
            string AssetBDetail = this.txtAssetBDetail.Text;
            string AssetCName = this.txtAssetCName.Text;
            decimal AssetCWorth = 0;
            try
            {
                AssetCWorth = decimal.Parse(this.txtAssetCWorth.Text);
            }
            catch { }
            string AssetCNote = this.txtAssetCNote.Text;
            string AssetCDetail = this.txtAssetCDetail.Text;


            Lawyer.Model.ArchiveBaseInfo model = new Lawyer.Model.ArchiveBaseInfo();
            model.ArchiveID = archive.ArchiveID;
            /**/
            model.FullName = FullName;
            if (this.txtBirthday.Text == "")
                model.Birthday = null;
            else
            {
                try
                {
                    model.Birthday = DateTime.Parse(this.txtBirthday.Text);
                }
                catch {
                    model.Birthday = null;
                }
            }
            model.Telphone = Telphone;
            model.Fax = Fax;
            model.Address = Address;
            model.CFullName = CFullName;
            if (this.txtCBirthday.Text == "")
                model.CBirthday = null;
            else
            {
                try
                {
                    model.CBirthday = DateTime.Parse(this.txtCBirthday.Text);
                }
                catch {
                    model.CBirthday = null;
                }
            }

            model.CTelphone = CTelphone;
            model.CFax = CFax;
            model.CEmail = CEmail;
            model.CRelation = CRelation;
            model.BankFunds = BankFunds;
            model.BankAccount = BankAccount;
            model.CaseSource = CaseSource;
            model.PersonalDetail = PersonalDetail;
            model.PersonalNote = PersonalNote;
            model.FatherName = FatherName;
            if (this.txtFatherDOB.Text == "")
                model.FatherDOB = null;
            else
            {
                try
                {
                    model.FatherDOB = DateTime.Parse(this.txtFatherDOB.Text);
                }
                catch {
                    model.FatherDOB = null;
                }
            }
            model.FatherNationality = FatherNationality;
            model.FatherContact = FatherContact;
            model.MotherName = MotherName;
            if (this.txtMotherDOB.Text == "")
                model.MotherDOB = null;
            else
            {
                try
                {
                    model.MotherDOB = DateTime.Parse(this.txtMotherDOB.Text);
                }
                catch {
                    model.MotherDOB = null;
                }
            }
            model.MotherNationality = MotherNationality;
            model.MotherContact = MotherContact;
            model.SisterName = SisterName;
            if (this.txtSisterDOB.Text == "")
                model.SisterDOB = null;
            else
            {
                try
                {
                    model.SisterDOB = DateTime.Parse(this.txtSisterDOB.Text);
                }
                catch {
                    model.SisterDOB = null;
                }
            }
            model.SisterContact = SisterContact;
            model.SisterNationality = SisterNationality;
            model.BrotherName = BrotherName;
            if (this.txtBrotherDOB.Text == "")
                model.BrotherDOB = null;
            else
            {
                try
                {
                    model.BrotherDOB = DateTime.Parse(this.txtBrotherDOB.Text);
                }
                catch {
                    model.BrotherDOB = null;
                }
            }
            model.BrotherNationality = BrotherNationality;
            model.BrotherContact = BrotherContact;
            model.AgreedFees = AgreedFees;
            model.FeesClusive = FeesClusive;
            model.FeesFundable = FeesFundable;
            model.PersonalStatement = PersonalStatement;
            model.LawyerNote = LawyerNote;
            model.AssetAName = AssetAName;
            model.AssetAWorth = AssetAWorth;
            model.AssetANote = AssetANote;
            model.AssetADetail = AssetADetail;
            model.AssetBName = AssetBName;
            model.AssetBWorth = AssetBWorth;
            model.AssetBNote = AssetBNote;
            model.AssetBDetail = AssetBDetail;
            model.AssetCName = AssetCName;
            model.AssetCWorth = AssetCWorth;
            model.AssetCNote = AssetCNote;
            model.AssetCDetail = AssetCDetail;
            model.UpdatedBy = Session["UserID"].ToString();
            model.UpdatedDate = DateTime.Now;

            Lawyer.BLL.ArchiveBaseInfo bll = new Lawyer.BLL.ArchiveBaseInfo();
            if (bll.Add(model))
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "success！", "archivelist.aspx");
            }
            else
            {
                Maticsoft.Common.MessageBox.ShowAndRedirect(this, "failed！", "archiveadd.aspx");
            }
            #endregion
        }

    }
}
